package com.ghh.swing;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 * @author hgu
 * 
 */
public class MyUIApplication {

	private JFrame			jframe;
	private BufferedImage	image;

	public MyUIApplication() {
		loadResource();
		initGUI();
	}

	private void loadResource() {
		try {
			image = ImageIO.read(MyUIApplication.class.getResource("/icons.jpg"));
		} catch (IOException e) {
			System.out.println("cant load resource icons.jpg");
		}
	}

	private void initGUI() {
		jframe = new JFrame("My App");
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jframe.setPreferredSize(new Dimension(800, 540));
		jframe.setMinimumSize(new Dimension(800, 540));

		initPanel();
		jframe.pack();
		jframe.setLocationRelativeTo(null); // locate the window at the center, put this after pack() or setSize()
		jframe.setVisible(true);
	}

	private void initPanel() {
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		jframe.setLayout(gbl);
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		{
			c.fill = GridBagConstraints.HORIZONTAL;
			c.gridwidth = GridBagConstraints.REMAINDER;
			c.weightx = 1.0;
			c.weighty = 0.0;
			addPanel(getTopPanel(), gbl, c);
		}
		{
			c.fill = GridBagConstraints.BOTH;
			c.gridwidth = GridBagConstraints.RELATIVE;
			c.weightx = 1.0;
			c.weighty = 1.0;
			addPanel("Panel 2", gbl, c);
		}
		{
			c.fill = GridBagConstraints.VERTICAL;
			c.gridwidth = GridBagConstraints.REMAINDER;
			c.weightx = 0.0;
			c.weighty = 1.0;
			JPanel p3 = addPanel("Panel 3", gbl, c);
			p3.setPreferredSize(new Dimension(200, 0));
		}
		{
			c.fill = GridBagConstraints.HORIZONTAL;
			c.gridwidth = GridBagConstraints.REMAINDER;
			c.weightx = 1.0;
			c.weighty = 0.0;
			JPanel p4 = addPanel("Panel 4", gbl, c);
			p4.setPreferredSize(new Dimension(0, 60));
		}
	}

	private JPanel getTopPanel() {
		JPanel p = new JPanel();
		p.setBorder(BorderFactory.createEtchedBorder());
		p.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
		p.add(createTopButton("button1", 0));
		p.add(createTopButton("button2", 1));
		p.add(createTopButton("button3", 2));
		p.add(createTopButton("button4", 3));
		p.setPreferredSize(new Dimension(0, 80));
		return p;
	}

	private JButton createTopButton(String label, int index) {
		JButton b = new JButton();
		// b.setText(label);
		b.setToolTipText(label);
		b.setIcon(new ImageIcon(getIcon(index)));
		b.setActionCommand(label);
		b.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MyUIApplication.this.action(e);
			}
		});
		b.setPreferredSize(new Dimension(156, 70));
		return b;
	}

	private Image getIcon(int index) {
		int sx = 25;
		int sy = 278;
		int w = 160;
		int h = 70;
		return image.getSubimage(sx + (index * w), sy, w - 4, h);
	}

	private void action(ActionEvent e) {
		System.out.println("perform " + e.getActionCommand());
		JButton b = (JButton) e.getSource();
		System.out.println(b.getSize());
		System.out.println(b.getIcon().getIconWidth() + "," + b.getIcon().getIconHeight());
	}

	private void addPanel(JPanel p, GridBagLayout gbl, GridBagConstraints c) {
		gbl.setConstraints(p, c);
		jframe.getContentPane().add(p);
	}

	private JPanel addPanel(String name, GridBagLayout gbl, GridBagConstraints c) {
		JPanel p = createPanel(name);
		gbl.setConstraints(p, c);
		jframe.getContentPane().add(p);
		return p;
	}

	private JPanel createPanel(String name) {
		JPanel p = new JPanel();
		p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), name));
		return p;
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new MyUIApplication();
			}
		});
	}
}
